import axios from 'axios';
import FormData from 'form-data'

export async function translateYoudao (
  text: string,
  // @ts-expect-error
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  sourceLang = 'zh',
  targetLang = 'en',
) {
  const formData = new FormData()
  formData.append('q', text)
  formData.append('le', targetLang)
  formData.append('t', 9)
  formData.append('client', 'web')
  formData.append('sign', '4d58e79991a4d93e72dc82041d4c7fe7')
  formData.append('keyfrom', 'webdict')

  try {
    const res = await axios.post(
      'https://dict.youdao.com/jsonapi_s',
      formData,
      {
        params: {
          doctype: 'json',
          jsonversion: 4,
        },
      },
    );
    return res.data
  } catch (error) {
    if (error instanceof Error) {
      console.error('Translation failed:', error.message);
    }
    return text; // 失败时返回原文
  }
}
